package com.hliushi.movie.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hliushi.movie.entity.MovieActor;
import com.hliushi.movie.entity.MovieDetail;
import com.hliushi.movie.entity.MovieDirector;
import com.hliushi.movie.entity.MoviePicClip;
import com.hliushi.movie.mapper.MovieDetailMapper;
import com.hliushi.movie.service.MovieActorService;
import com.hliushi.movie.service.MovieDetailService;
import com.hliushi.movie.service.MovieDirectorService;
import com.hliushi.movie.service.MoviePicClipService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class MovieDetailServiceImpl extends ServiceImpl<MovieDetailMapper, MovieDetail> implements MovieDetailService {


    @Resource
    private MovieActorService movieActorService;

    @Resource
    private MovieDirectorService movieDirectorService;

    @Resource
    private MoviePicClipService moviePicClipService;

    @Override
    public List<MovieActor> getAllActorByMovieId(int movieId) {
        MovieDetail movieDetail = super.getById(movieId);

        List<MovieActor> res = null;
        if (movieDetail != null && StringUtils.isNotBlank(movieDetail.getActorIds())) {
            String actorIds = movieDetail.getActorIds();
            String[] actorIdArr = actorIds.split(",");
            res = Arrays.stream(actorIdArr)
                    .map(actorId -> movieActorService.getById(actorId))
                    .collect(Collectors.toList());
            log.info("电影Id: {}, 其所有演员为: {}", movieId, res);
        }
        return res;
    }

    @Override
    public MovieDirector getByMovieId(int movieId) {
        MovieDetail movieDetail = super.getById(movieId);

        if (movieDetail != null && Objects.nonNull(movieDetail.getDirectorId())) {
            Long directorId = movieDetail.getDirectorId();
            MovieDirector movieDirector = movieDirectorService.getById(directorId);
            log.info("电影Id: {}, 其的导演信息为: {}", movieId, movieDirector);
            return movieDirector;
        }
        return null;
    }

    @Override
    public List<MoviePicClip> getByMovieIdAndType(Integer movieId, Integer type) {
        List<MovieDetail> movieDetailList = super.list(Wrappers.<MovieDetail>lambdaQuery()
                .eq(Objects.nonNull(movieId), MovieDetail::getId, movieId)
                .eq(Objects.nonNull(type), MovieDetail::getType, type)
        );
        Set<MoviePicClip> res = new HashSet<>();
        if (CollectionUtils.isNotEmpty(movieDetailList)) {
            movieDetailList.stream()
                    .filter(item -> StringUtils.isNotBlank(item.getTuiVideos()))
                    .forEach(item -> {
                        String[] tuiIds = item.getTuiVideos().split(",");
                        List<MoviePicClip> collect = Arrays.stream(tuiIds)
                                .map(tuiId -> moviePicClipService.getById(tuiId))
                                .collect(Collectors.toList());
                        // 使用set不可重复的特性, 对获取的数据项进行去重
                        res.addAll(collect);
                    });

            return new ArrayList<>(res);
        }
        return null;
    }
}
